package com.yueban.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.yueban.R;
import com.yueban.model.Comment;

import java.util.List;

/**
 * Created by guan on 17-8-13.
 */

public class HomeCommentListAdapter extends BaseAdapter {
    private List<Comment> comments;
    private LayoutInflater layoutInflater;


    public List<Comment> getComments(){
        return comments;
    }
    public HomeCommentListAdapter(Context context, List<Comment> comments) {
        layoutInflater = LayoutInflater.from(context);
        this.comments = comments;
    }

    @Override
    public int getCount() {
        if (comments != null)
            return comments.size();
        return 0;
    }

    @Override
    public Object getItem(int i) {
        return comments.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        MyViewHolder myViewHolder;
        if(view == null){
            view=layoutInflater.inflate(R.layout.item_home_content_comments, null);
            myViewHolder = new MyViewHolder(view);
            view.setTag(myViewHolder);
        }else {
            myViewHolder = (MyViewHolder) view.getTag();
        }
        myViewHolder.commentator.setText(comments.get(i).getUserName());
        myViewHolder.comment.setText(comments.get(i).getCommentText());
        return view;
    }

    private class MyViewHolder {
        private TextView commentator;
        private TextView comment;

        private MyViewHolder(View view) {
            commentator = (TextView) view.findViewById(R.id.commentator);
            comment = (TextView) view.findViewById(R.id.comment_text);
        }
    }
}
